﻿using System;
using System.Collections.Generic;
using System.Reflection;


public static class CustomAttributeCache
{
    private readonly static Dictionary<Type, object[]> typeCustomAttributes;
    private readonly static Dictionary<FieldInfo, object[]> fieldCustomAttributes;

    static CustomAttributeCache()
    {
        CustomAttributeCache.typeCustomAttributes = new Dictionary<Type, object[]>();
        CustomAttributeCache.fieldCustomAttributes = new Dictionary<FieldInfo, object[]>();
    }

    public static object[] GetCustomAttributes(Type type)
    {
        object[] customAttributes;
        if (!CustomAttributeCache.typeCustomAttributes.TryGetValue(type, out customAttributes))
        {
            customAttributes = type.GetCustomAttributes(true);
            CustomAttributeCache.typeCustomAttributes.Add(type, customAttributes);
        }
        return customAttributes;
    }

    public static object[] GetCustomAttributes(FieldInfo field)
    {
        object[] customAttributes;
        if (!CustomAttributeCache.fieldCustomAttributes.TryGetValue(field, out customAttributes))
        {
            customAttributes = field.GetCustomAttributes(true);
            CustomAttributeCache.fieldCustomAttributes.Add(field, customAttributes);
        }
        return customAttributes;
    }
}